package com.util.seo;


import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class R_WFile {

	/**
	 * 写文件
	 * @param filePath
	 * @param sbuffer
	 */
	public static void writerFile(String filePath,StringBuffer sbuffer) {
		FileWriter fw = null;
		BufferedWriter bw = null;
		try {
			fw = new FileWriter(filePath);
			bw = new BufferedWriter(fw);    //将缓冲对文件的输出
			bw.write(sbuffer.toString());			
			bw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {				
				bw.close();
				fw.close();				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	
	/**
	 * 写文件
	 * @param filePath
	 * @param sbuffer
	 */
	public static void cDirwriterFile(String filePath,StringBuffer sbuffer) {
		FileWriter fw = null;
		BufferedWriter bw = null;
		try {
//			String dir = filePath.substring(0,filePath.lastIndexOf("\\"));
			String dir = filePath.substring(0,filePath.lastIndexOf("/"));
			File f = new File(dir);
			if(!f.exists() && !f.isDirectory())
				f.mkdirs();
			fw = new FileWriter(filePath);
			bw = new BufferedWriter(fw);    //将缓冲对文件的输出
			bw.write(sbuffer.toString());			
			bw.flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {				
				bw.close();
				fw.close();				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
	
	public BufferedReader bufread;
	public BufferedWriter bufwriter;
	File writefile;
	String filepath, filecontent, read;
	String readStr = "";

	// 从文本文件中读取内容
	public String readfile(String path) {
		try {
			filepath = path; // 得到文本文件的路径
			File file = new File(filepath);
			FileReader fileread = new FileReader(file);
			bufread = new BufferedReader(fileread);
			while ((read = bufread.readLine()) != null) {
				read = read + " \r\n ";
				readStr = readStr + read;
			}
		} catch (Exception d) {
			System.out.println(d.getMessage());
		}
		return readStr; // 返回从文本文件中读取内容
	}

	// 向文本文件中写入内容
	public void writefile(String path, String content, boolean append) {
		try {
			boolean addStr = append; // 通过这个对象来判断是否向文本文件中追加内容
			filepath = path; // 得到文本文件的路径
			filecontent = content; // 需要写入的内容
			writefile = new File(filepath);
			if (writefile.exists() == false) // 如果文本文件不存在则创建它
			{
				writefile.createNewFile();
				writefile = new File(filepath); // 重新实例化
			}
			FileWriter filewriter = new FileWriter(writefile, addStr);
			// 删除原有文件的内容
			java.io.RandomAccessFile file = new java.io.RandomAccessFile(path," rw ");
			file.setLength(0);
			// 写入新的文件内容
			filewriter.write(filecontent);
			filewriter.close();
			filewriter.flush();
		} catch (Exception d) {
			System.out.println(d.getMessage());
		}
	}

	public static void main(String[] args) throws Exception {
		R_WFile parse = new R_WFile();
		String filecontent = parse.readfile(" c:/applicationContext.xml ");
		parse.writefile(" c:/applicationContext.xml ", filecontent, true);

	}
}
